package com.beidouin.GnssMain.utils;

/**
 * Created by yuyang on 16/7/21.
 */

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.widget.ArrayAdapter;

import com.beidouin.GnssMain.R;
import com.beidouin.GnssMain.sample.SerialPortActivity;
import com.r0adkll.postoffice.PostOffice;
import com.r0adkll.postoffice.model.Delivery;
import com.r0adkll.postoffice.model.Design;
import com.r0adkll.postoffice.styles.ListStyle;

/**
 * Created by game-netease on 15/7/23.
 */
public class DialogUtil {
    private static Design sMtrlDesign = Design.MATERIAL_LIGHT;

    public static void showConfirmDialog(Activity activity, String title, String message,
                                         ButtonSettings btnSettings, final DialogCallback callback) {
        showConfirmDialog(activity, title, message, btnSettings, null, callback);
    }

    public static void showConfirmDialog(Activity activity, String title, String message,
                                         ButtonSettings btnSettings, final int buttonTextResIds[],
                                         final DialogCallback callback) {
        Delivery.Builder builder = PostOffice.newMail(activity)
                .setTitle(title)
                .setMessage(message)
                .setThemeColor(activity.getResources().getColor(R.color.app_main))
                .setDesign(sMtrlDesign)
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .showKeyboardOnDisplay(false);

        if (btnSettings != null) {
            initButtons(btnSettings, buttonTextResIds, callback, builder);
        }

        if (activity instanceof SerialPortActivity) {
            if (!((SerialPortActivity) activity).hasResumed()) {
                return;
            }
        }
        builder.show(activity.getFragmentManager());
    }

    public static void showListDialog(Activity activity, String title, ArrayAdapter<String> adapter,
                                      ListStyle.OnItemAcceptedListener listener, ButtonSettings btnSettings, final int buttonTextResIds[],
                                      final DialogCallback callback){
        Delivery.Builder delivery = PostOffice.newMail(activity)
                .setTitle(title)
                .setThemeColor(activity.getResources().getColor(R.color.app_main))
                .setDesign(sMtrlDesign)
                .setCancelable(true)
                .setCanceledOnTouchOutside(false)
                .setStyle(new ListStyle.Builder(activity)
                        .setDividerHeight(2)
                        .setOnItemAcceptedListener(listener)
                        .build(adapter));
        if (btnSettings != null) {
            initButtons(btnSettings, buttonTextResIds, callback, delivery);
        }
        delivery.build();
        delivery.show(activity.getFragmentManager());
    }

    private static void initButtons(ButtonSettings btnSettings, final DialogCallback callback,
                                    Delivery.Builder builder) {
        initButtons(btnSettings, null, callback, builder);
    }

    private static void initButtons(ButtonSettings btnSettings, final int buttonTextResIds[],
                                    final DialogCallback callback, Delivery.Builder builder) {
        final int[] finalButtonTextResIds;
        if (buttonTextResIds == null || buttonTextResIds.length != 2) {
            finalButtonTextResIds = new int[]{android.R.string.ok, android.R.string.cancel};
        } else {
            finalButtonTextResIds = buttonTextResIds;
        }
        if (btnSettings == ButtonSettings.POSITIVE_ONLY || btnSettings == ButtonSettings.POSITIVE_AND_NEGATIVE) {
            builder.setButton(Dialog.BUTTON_POSITIVE, finalButtonTextResIds[0], new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (callback != null) {
                        callback.onPositiveButtonClicked();
                    }
                    if (dialog != null) dialog.dismiss();
                }
            });
        }
        if (btnSettings == ButtonSettings.NEGATIVE_ONLY || btnSettings == ButtonSettings.POSITIVE_AND_NEGATIVE) {
            builder.setButton(Dialog.BUTTON_NEGATIVE, finalButtonTextResIds[1], new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (callback != null) {
                        callback.onNegativeButtonClicked();
                    }
                    if (dialog != null) dialog.dismiss();
                }
            });
        }
    }

    public enum ButtonSettings {
        NONE, POSITIVE_ONLY, NEGATIVE_ONLY, POSITIVE_AND_NEGATIVE
    }

    public interface DialogCallback {
        public void onNegativeButtonClicked();

        public void onPositiveButtonClicked();

    }
}

